package cn.zj.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.zj.logistics.pojo.BaseData;
import cn.zj.logistics.pojo.BaseDataExample;
import cn.zj.logistics.pojo.BaseDataExample.Criteria;
import cn.zj.logistics.pojo.MessageObject;
import cn.zj.logistics.service.BaseDataService;

@Controller
@RequestMapping("/baseData")
public class BaseDataController {
	
	
	@Autowired
	private BaseDataService baseDataService;
	
	

	// 跳转到 baseData 管理页面
	@RequiresPermissions("baseData:baseDataPage")
	@RequestMapping("/baseDataPage")
	public String baseDataPage() {
		return "baseDataPage";
	}
	
	
	
	@ResponseBody
	@RequiresPermissions("baseData:update")
	@RequestMapping("/update")
	public MessageObject update(BaseData baseData) {
		
		
		int row = baseDataService.updateByPrimaryKeySelective(baseData);
		
		MessageObject mo = MessageObject.creatMO(0, "修改数据失败，请联系管理员");
		if(row == 1) {
			mo = MessageObject.creatMO(1, "修改数据成功");
		}
		
		return mo;
		
	}
	
	
	@ResponseBody
	@RequiresPermissions("baseData:insert")
	@RequestMapping("/insert")
	public MessageObject insert(BaseData baseData) {
		
		int row = baseDataService.insert(baseData);
		
		MessageObject mo = MessageObject.creatMO(0, "新增数据失败，请联系管理员");
		if(row == 1) {
			mo = MessageObject.creatMO(1, "新增数据成功");
		}
		
		return mo;
		
	}
	
	
	/*
	 * jackson+@ResponseBody 方法直接返回json格式的数据
	 * 
	 */
	
	@ResponseBody
	@RequiresPermissions("baseData:list")
	@RequestMapping("/list")
	public PageInfo<BaseData> list(@RequestParam(defaultValue = "1")Integer pageNum,
								@RequestParam(defaultValue = "10")Integer pageSize,
								String keyword) {
		PageHelper.startPage(pageNum, pageSize);

		BaseDataExample example = new BaseDataExample();

		//开始条件查询
		if(StringUtils.isNotBlank(keyword)) {
			
			Criteria criteria1 = example.createCriteria();
			criteria1.andBaseNameLike("%"+keyword+"%");
			
			//根据父分类的名称搜索
			Criteria criteria2 = example.createCriteria();
			criteria2.andParentNameLike("%"+keyword+"%");
			
			//OR关系
			example.or(criteria2);
		}
		
		
		List<BaseData> baseDatas = baseDataService.selectByExample(example);

		PageInfo<BaseData> pageInfo = new PageInfo<>(baseDatas);
		
		return pageInfo;
	}
	
	
	@ResponseBody
	@RequiresPermissions("baseData:delete")
	@RequestMapping("/delete")
	public MessageObject delete(Long baseId) {
		
		
		/*
		 * 删除之前判断 ：是否还有 基础数据的父id是 当前要删除的id
		 * 
		 * 此数据是分类数据，并且还有子数据，不能删除
		 * 
		 */
		
		BaseDataExample example = new BaseDataExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(baseId);
		List<BaseData> children = baseDataService.selectByExample(example);
		
		if(children.size() > 0) {
			
			MessageObject mo = MessageObject.creatMO(0, "该基础数据还有子数据，不能删除！！");
			return mo;
		}
		
		
		
		int row = baseDataService.deleteByPrimaryKey(baseId);
		
		MessageObject mo = MessageObject.creatMO(0, "删除数据失败，请联系管理员！！");
		
		//删除成功
		if(row == 1) {
			mo = MessageObject.creatMO(1, "删除数据成功！！！");
		}
		
		return mo;
	}
	
	
	
	
	/*
	 * 新增或者修改跳转的编辑页面
	 * 
	 */
	
	@RequestMapping("/baseDataEdit")
	public String baseDataEdit(Model m,Long baseId) {
		
		
		/*
		 * 如果是修改，有baseId，要根据id查询出对应的数据并共享让jsp页面回显
		 */
		if(baseId != null) {
			BaseData baseData = baseDataService.selectByPrimaryKey(baseId);
			m.addAttribute("baseData", baseData);
		}
		//只有父id为空的数据才是 类别数据
		BaseDataExample example = new BaseDataExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdIsNull();
		//查询父分类数据
		List<BaseData> parents = baseDataService.selectByExample(example);
		
		m.addAttribute("parents", parents);
		
		
		return "baseDataEdit";
	}
	
	
	
	
	
	
}
